import 'package:flutter/material.dart';
import 'package:flutter_app_radial_progress/themes/colors.dart';

import 'graph_data.dart';

class Graph extends StatelessWidget {

  AnimationController animationController;
  final double percentage= 0.2;
  final double height;
  final List<GraphData> graphDatas;
  Graph({this.animationController,this.height,this.graphDatas});

  List<Widget> getGraphBars(height,List<GraphData> values,AnimationController animationController){
    List<Widget> rev =  List<Widget>();
    if(values== null) {
      return rev;
    }
      //计算最大值并计算百分比
    GraphData maxGraphData =
    values.reduce((current,next)=>current.compareTo(next)>1?current:next);
    print(maxGraphData.value);
    values.forEach((item){
        rev.add(GraphBar(height:height,percentage: item.value/maxGraphData.value,label: item.name,
            animationController:animationController));
      });
    return rev;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Padding(
        padding: const EdgeInsets.only(left:18.0,right:18.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          mainAxisSize: MainAxisSize.max,
          children:getGraphBars(height,this.graphDatas,animationController)
        ),
      ),
    );
  }
}

class GraphBar extends StatefulWidget {
  final double height;
  final double percentage;
  final String label ;
  final AnimationController animationController;
  GraphBar({this.height,this.percentage,this.label,this.animationController});

  @override
  _GraphBarState createState() => _GraphBarState();
}

class _GraphBarState extends State<GraphBar> {
  Animation<double> percentageAnimation ;

  @override
  void initState() {
    super.initState();
    percentageAnimation = Tween(begin: 0.0,end: widget.percentage)
    .animate(widget.animationController);
    widget.animationController.addListener((){
      setState(() {

      });
    });
  }
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Text(widget.label),
        SizedBox(height: 10.0,),
        CustomPaint(
          painter: BarPainter(percentage:percentageAnimation.value),
          child:   Container(height: widget.height,
            width: 5.0,)
        ),
      ],
    );
  }
}

class BarPainter extends CustomPainter{

  final double percentage ;
  BarPainter({this.percentage});

  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    Paint greyPaint = Paint()
        ..color = greyColor
        ..strokeCap = StrokeCap.round
        ..strokeWidth = 5.0;

    Offset topPoint = Offset(0, 0);
    Offset bottomPoint = Offset(0,size.height);
    Offset centerPoint = Offset(0,size.height/2);

    canvas.drawLine(topPoint, bottomPoint, greyPaint);

    Paint filledPaint = Paint()
      ..shader = LinearGradient(colors:[
        Colors.pink.shade500,Colors.blue.shade500
      ],
      begin:Alignment.topCenter,end: Alignment.bottomLeft).createShader(Rect.fromPoints(topPoint, bottomPoint))
      ..strokeCap = StrokeCap.round
      ..strokeWidth = 5.0;

      double filedHeight = percentage* size.height;
      double filledHalfHeight = filedHeight/2;
      print(filledHalfHeight);
      canvas.drawLine(centerPoint, Offset(0, centerPoint.dy - filledHalfHeight), filledPaint);
    canvas.drawLine(centerPoint, Offset(0, centerPoint.dy + filledHalfHeight), filledPaint);


  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }
}
